/**
 *HomeActivity.java
 *2011-9-18 下午10:00:06
 *Touch Android
 *http://bbs.droidstouch.com
 */
package com.droidstouch.iweibo.ui;

import java.util.HashMap;
import java.util.List;

import weibo4android.Status;
import android.R.string;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.droidstouch.iweibo.R;
import com.droidstouch.iweibo.adapter.WeiboAdapter;
import com.droidstouch.iweibo.app.Preferences;
import com.droidstouch.iweibo.bean.Task;
import com.droidstouch.iweibo.logic.MainService;

/**
 * @author <a href="http://bbs.droidstouch.com">Touch Android</a>
 *
 */
public class HomeActivity extends BaseActitity implements IWeiboActivity
{


//	private static final String TAG="HomeActivity";
	
	
	//加载View
	private View progresView;
	private View titleView;
	private ListView weiboListView;
	
	private View loadMoreView;
	private LinearLayout llytLoading;
	private TextView txtMore;
	private long   maxId=0;
	private WeiboAdapter adapter;
	private Button btnRefresh;
	private Button btnWriter;
	private static final int MODE_NEW=0;
	private static final int MODE_MORE=1;
	private int mode=MODE_NEW;
	
	
	
	public static final String STATUS_ID="status_id";
	
	
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.home);
		
		
	
	  init();
	  
	  loadMoreView.setOnClickListener(new LoadMoreOnClickListener());
	  
	  weiboListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id)
			{

				Intent intent = 	new Intent(HomeActivity.this, WeiboDetailActiivity.class);
				intent.putExtra(STATUS_ID, id);
					
				HomeActivity.this.startActivityForResult(intent, 0);
				
			}
	  	
	  	
		});
	
		
	}


	/**
	 * 添加获取微博信息任务
	 */
	private void newTask()
	{
		
		HashMap<String, Object> params = new HashMap<String, Object>(1);
		
		params.put("maxId", maxId);
		
		Task task = new Task(Task.WEIBO_FRIEDNS_TIMELINE,params);
		
		MainService.newTask(task);
		
	}


	public void init()
	{
		
		weiboListView = (ListView) this.findViewById(R.id.lv_weibos);
		progresView = this.findViewById(R.id.layout_progress);
		titleView = this.findViewById(R.id.layout_title_bar);
		loadMoreView = View.inflate(this, R.layout.more_layout, null);
		
		btnRefresh = (Button) titleView.findViewById(R.id.btn_refresh);
		btnWriter =  (Button) titleView.findViewById(R.id.btn_writer);
		
		llytLoading = (LinearLayout) loadMoreView.findViewById(R.id.llyt_loading);
		txtMore = (TextView) loadMoreView.findViewById(R.id.txt_more);
		
		
	  ((TextView)titleView.findViewById(R.id.txt_wb_title)).setText(MainService.nowUser.getUserName());
		
	  
	  btnWriter.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				HomeActivity.this.startActivityForResult(new Intent(HomeActivity.this, NewWeiboActivity.class), 0);
			}
		});
	  
	  btnRefresh.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				Animation animation = AnimationUtils.loadAnimation(HomeActivity.this, R.anim.ref_anim);
				btnRefresh.startAnimation(animation);
				
				loadNew();
				
			}
		});
	  
	  
		weiboListView.addFooterView(loadMoreView, null, true);
		MainService.addActivity(this);
		
		
		newTask();
		
	}

	
	
	
	

	@SuppressWarnings("unchecked")
	public void refresh(Object... params)
	{
		
		List<Status> status = (List<Status>) params[0];
		btnRefresh.clearAnimation();
		progresView.setVisibility(View.GONE);
		
		
		if(status != null && status.size() >0)
		{
			maxId = Long.parseLong(status.get(status.size() -1).getMid())-1;
			
			if(mode == MODE_NEW)
			{
				WeiboAdapter adapter = new WeiboAdapter(this, status);
				weiboListView.setAdapter(adapter);
			}
			else
			{
				if(adapter == null)
				{
					
					WeiboAdapter adapter = new WeiboAdapter(this, status);
					weiboListView.setAdapter(adapter);
				}
				else
				{
					adapter.refrensh(status);
					showLoadingControl(false);
					
					weiboListView.setSelection(adapter.getCount() - Preferences.PAGE_SIZE);
					
				}
			}
		
		}
		else
		{
			// 提示无微博信息
		}
		
	}
	

	/**
	 * 加载更多
	 */
	
	private void loadMore()
	{
		
		mode = MODE_MORE;
		newTask();
	}
	

	/**
	 * 刷新
	 */
	private void loadNew()
	{
		
		maxId = -1;
		mode = MODE_NEW;
		newTask();
	}
	
	
	
	
	
	private void showLoadingControl(boolean isShow)
	{
		
		txtMore.setVisibility(isShow?View.GONE:View.VISIBLE);
		llytLoading.setVisibility(isShow?View.VISIBLE:View.GONE);
		
	}
	
	
	final class LoadMoreOnClickListener implements OnClickListener
	{
		
		public void onClick(View v)
		{
			showLoadingControl(true);
			loadMore();
		}
	}


	
	
}
